import { HttpException, HttpStatus } from '@nestjs/common';

import type { IErrorInterceptData0 } from '@/types/globalType';
import otherConfig from '@/config/otherConfig';
/**
 * state == 0 的情况，主要是 token 验证失败、代码执行错误
 */
export function throwHttpException(
  info: IErrorInterceptData0,
  state?: HttpStatus,
) {
  throw new HttpException(
    {
      type: '0',
      data: {
        errCode: info.errCode,
        errMsg: info.errMsg,
        errStack: info.errStack,
      },
    },
    // HttpStatus.OK,
    state ?? HttpStatus.OK,
  );
}
/**
 * state == 1 的情况，主要是前端入参错误、接口返回
 */
export function throwHttpException1(
  data: { state: '0' | '1'; msg: string; data?: any },
  state?: HttpStatus,
) {
  throw new HttpException(
    {
      type: '1',
      data,
    },
    // HttpStatus.OK,
    state ?? HttpStatus.OK,
  );
}

// export function parseTokenPayload(this: any, token: string) {
//   return this.jwtService.verify(token, {
//     secret: otherConfig.tokenSecret, //
//   });
// }

export function parseTokenPayload(curThis, token) {
  return curThis.jwtService.verify(token, { secret: otherConfig.tokenSecret });
}
